export default defineEventHandler(async (event) => {
  const { limit = 6 } = getQuery(event)
  
  // 热门文档数据（使用 product 格式，方便复用组件）
  // 使用 picsum.photos 提供随机占位图片，后期替换为真实图片
  const docs = [
    {
      id: '1',
      title: 'Vue3 Composition API 完全指南',
      description: '深入学习 Vue3 的 Composition API，包含响应式原理、setup 语法、组合式函数等核心概念',
      price: 0, // 免费文档
      originalPrice: 0,
      cover: 'https://picsum.photos/seed/vue3/600/400',
      category: 'frontend',
      tags: ['Vue3', 'Composition API', '前端'],
      tech: ['Vue3', 'TypeScript', 'Composition API'],
      difficulty: 'intermediate',
      rating: 4.9,
      reviews: 328,
      downloads: 5680,
      createdAt: '2025-01-01',
      updatedAt: '2025-01-10',
    },
    {
      id: '2',
      title: 'TypeScript 进阶实战教程',
      description: '从基础到进阶的 TypeScript 完整教程，涵盖类型系统、泛型、装饰器等高级特性',
      price: 0,
      originalPrice: 0,
      cover: 'https://picsum.photos/seed/typescript/600/400',
      category: 'language',
      tags: ['TypeScript', '进阶', '类型系统'],
      tech: ['TypeScript', 'JavaScript', 'Node.js'],
      difficulty: 'advanced',
      rating: 4.8,
      reviews: 256,
      downloads: 4320,
      createdAt: '2025-01-05',
      updatedAt: '2025-01-12',
    },
    {
      id: '3',
      title: 'Nuxt3 SSR 全栈开发实战',
      description: '学习使用 Nuxt3 构建 SSR 应用，包含路由、状态管理、SEO 优化等实战技巧',
      price: 0,
      originalPrice: 0,
      cover: 'https://picsum.photos/seed/nuxt3/600/400',
      category: 'fullstack',
      tags: ['Nuxt3', 'SSR', 'SEO'],
      tech: ['Nuxt3', 'Vue3', 'SSR', 'SEO'],
      difficulty: 'intermediate',
      rating: 4.7,
      reviews: 189,
      downloads: 3450,
      createdAt: '2025-01-08',
      updatedAt: '2025-01-14',
    },
    {
      id: '4',
      title: 'NestJS 企业级后端开发',
      description: '企业级 NestJS 后端开发教程，涵盖模块化设计、依赖注入、数据库集成等内容',
      price: 0,
      originalPrice: 0,
      cover: 'https://picsum.photos/seed/nestjs/600/400',
      category: 'backend',
      tags: ['NestJS', 'Node.js', '后端'],
      tech: ['NestJS', 'TypeScript', 'TypeORM', 'MySQL'],
      difficulty: 'advanced',
      rating: 4.9,
      reviews: 412,
      downloads: 6780,
      createdAt: '2025-01-03',
      updatedAt: '2025-01-11',
    },
    {
      id: '5',
      title: 'React Hooks 最佳实践',
      description: '深入理解 React Hooks 的使用场景和最佳实践，包含自定义 Hooks 开发技巧',
      price: 0,
      originalPrice: 0,
      cover: 'https://picsum.photos/seed/react-hooks/600/400',
      category: 'frontend',
      tags: ['React', 'Hooks', '最佳实践'],
      tech: ['React', 'JavaScript', 'Hooks'],
      difficulty: 'intermediate',
      rating: 4.6,
      reviews: 298,
      downloads: 4890,
      createdAt: '2025-01-06',
      updatedAt: '2025-01-13',
    },
    {
      id: '6',
      title: '前端性能优化实战指南',
      description: '全面的前端性能优化指南，涵盖加载优化、渲染优化、网络优化等各个方面',
      price: 0,
      originalPrice: 0,
      cover: 'https://picsum.photos/seed/performance/600/400',
      category: 'frontend',
      tags: ['性能优化', '前端', '最佳实践'],
      tech: ['性能优化', 'Webpack', 'Vite', 'CDN'],
      difficulty: 'advanced',
      rating: 5.0,
      reviews: 567,
      downloads: 8920,
      createdAt: '2025-01-10',
      updatedAt: '2025-01-15',
    },
  ]
  
  // 根据 limit 返回对应数量
  const limitNum = Number(limit)
  const selectedDocs = docs.slice(0, limitNum)
  
  // 为每个文档添加 images 字段
  const items = selectedDocs.map(doc => ({
    ...doc,
    difficulty: doc.difficulty as 'beginner' | 'intermediate' | 'advanced',
    images: [
      doc.cover,
      doc.cover.replace('/600/400', '/600/400?random=1'),
      doc.cover.replace('/600/400', '/600/400?random=2'),
      doc.cover.replace('/600/400', '/600/400?random=3'),
    ]
  }))
  
  return {
    code: 200,
    message: 'success',
    data: {
      items,
      pagination: {
        page: 1,
        pageSize: limitNum,
        total: docs.length,
        totalPages: Math.ceil(docs.length / limitNum)
      }
    }
  }
})
